﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PodcastToMP3
{
    public class Atom : IAtom
    {
        public long Start { get; set; }

        public long End { get; set; }

        public System.IO.Stream BaseStream { get; set; }

        public long Length
        {
            get { return End - Start; }
            set { this.End = this.Start + value; }
        }

        public string Name
        {
            get
            {
                return this.BaseStream.Read<string>(this.Start + 4, 4);
            }
        }

        public string RawData
        {
            get
            {
                return this.BaseStream.Read<string>(this.Start, this.End);
            }
        }

        public IAtom Parent { get; set; }

        public Atom(System.IO.Stream BaseStream, IAtom Parent, long Start)
        {
            this.BaseStream = BaseStream;
            this.Parent = Parent;
            this.Start = Start;
            this.Length = this.BaseStream.Read<int>(this.Start, 4);
        }

        public IEnumerator<IAtom> GetEnumerator()
        {
            return new Mp4IEnumerator(this.BaseStream, this, this.Start, this.End);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
